#include <iostream>
#include <unistd.h>
#include <sys/types.h>
/*------------- 匿名管道读写通讯 -----------*/


int main()
{

    // 1、创建管道
    int pipefd[2];
    if(pipe(pipefd) == 0)
        perror("pipe");
    else
        std::cout << "pipe success" << std::endl;
    // 2、创建父子进程
    pid_t id = fork();
    if(id == -1)
        perror("fork");

    std::cout << "fork success" << std::endl;    
    // 3、父进程读  子进程写
    // 3.1、子进程写
    if(id == 0)
    {
        close(pipefd[0]);
        write(pipefd[1],"hello world",12);
        close(pipefd[1]);
    }

    // 3.2、父进程读
    else
    {
        close(pipefd[1]);
        char buff[20];
        std::cout << "未读" << buff << std::endl;
        read(pipefd[0],buff,sizeof(buff));
        std::cout << buff << std::endl;
        
    }

    return 0;
}

